package com.gd.coreservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gd.coreservice.entity.Admin;
import com.gd.coreservice.entity.Users;
import com.gd.coreservice.entity.vo.AdminVo;
import com.gd.coreservice.entity.vo.UsersVo;
import com.gd.coreservice.mapper.UsersMapper;
import com.gd.coreservice.service.UsersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2024-03-23
 */
@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements UsersService {

    @Override
    public Users findOne(String id) {
        return baseMapper.selectById(id);
    }

    @Override
    public UsersVo login(String username, String password) {
        try{
            QueryWrapper<Users> usersQueryWrapper =  new QueryWrapper<>();
            usersQueryWrapper.eq("username",username)
                    .eq("password",password);
            Users users = baseMapper.selectOne(usersQueryWrapper);
            UsersVo usersVo = new UsersVo();
            BeanUtils.copyProperties(users,usersVo);
            return usersVo;
        }
        catch (Exception e)
        {
            return null;
        }
    }

    @Override
    public String findId(String username) {
        QueryWrapper<Users> usersQueryWrapper =  new QueryWrapper<>();
        usersQueryWrapper.eq("username",username);
        Users users = baseMapper.selectOne(usersQueryWrapper);
        return users.getUid();
    }

    @Override
    public boolean register(String username, String password) {
        Users users = new Users();
        users.setUsername(username);
        users.setPassword(password);
        try{
            if (0!=baseMapper.insert(users)){
                return true;
            }
        }
        catch (Exception e){
        }
        return false;
    }

    @Override
    public boolean updatePassword(String uid, String password, String newpassword) {
        try {
            Users users = new Users();
            users.setUid(uid);
            users.setPassword(newpassword);
            UpdateWrapper<Users> UpdateWrapper = new UpdateWrapper<>();
            UpdateWrapper.eq("uid",uid);
            UpdateWrapper.eq("password",password);
            int result = baseMapper.update(users,UpdateWrapper);
            if (0!=result){
                return true;
            }
            else return false;
        }catch (Exception e){
            return false;
        }
    }

    @Override
    public List<Users> findAll() {
        return baseMapper.selectList(null);
    }

    @Override
    public boolean insertUsers(Users users) {
        boolean istrue = false;
        try{
            if (0!=baseMapper.insert(users)){
                istrue = true;
            }
            return istrue;
        }
        catch (Exception e){
            return istrue;
        }
    }

    @Override
    public boolean updateUser(Users users) {
        boolean istrue = false;
        try{
            UpdateWrapper<Users> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("uid",users.getUid());
            if (0!=baseMapper.update(users,updateWrapper)){
                istrue = true;
            }
            return istrue;
        }
        catch (Exception e){
            return istrue;
        }
    }

    @Override
    public Integer deleteUserById(String uid) {
        return baseMapper.deleteById(uid);
    }

    @Override
    public Integer deleteUserBatch(List<String> uids) {
        return baseMapper.deleteBatchIds(uids);
    }

    @Override
    public Map<String, Object> getUserPageData(Integer pageNum, Integer pageSize, String username) {
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        Page<Users> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Users> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.like("username",username);
        IPage<Users> usersIPage = baseMapper.selectPage(page, objectQueryWrapper);

        objectObjectHashMap.put("data",usersIPage.getRecords());
        objectObjectHashMap.put("total",usersIPage.getTotal());
        return objectObjectHashMap;
    }

    @Override
    public int getAllTotal() {
        return baseMapper.selectCount(null);
    }

}



